package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Autowired
    private SetmealDao setmealDao;

    /**
     * 分页按条件查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //开启分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //按条件查询
        Page<Setmeal> setmealPage = setmealDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult(setmealPage.getTotal(), setmealPage.getResult());
    }

    /**
     * 添加套餐 和 添加检查组信息
     *
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //添加套餐
        setmealDao.add(setmeal);
        //获取添加套餐的id
        Integer id = setmeal.getId();
        //添加套餐和检查组信息到中间表
        for (Integer checkgroupId : checkgroupIds) {
            setmealDao.setSetmealAndCheckGroup(id, checkgroupId);
        }
    }

    /**
     * 根据id查询套餐组
     *
     * @param id
     */
    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    /**
     * 根据id套餐 查询 检查组
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckGroupBySetmealId(Integer id) {
        return setmealDao.findCheckGroupBySetmealId(id);
    }

    /**
     * 修改套餐信息
     *
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        //1.先修改套餐，往t_setmeal添加一条数据
        setmealDao.edit(setmeal);
        //删除该套餐所有关联检查组
        setmealDao.deleteAssocication(setmeal.getId());
        //添加到中间表
        for (Integer checkgroupId : checkgroupIds) {
            setmealDao.setSetmealAndCheckGroup(setmeal.getId(), checkgroupId);
        }
    }

    //查找所有套餐信息
    @Override
    public List<Setmeal> findAll() {
        List<Setmeal> list = setmealDao.findAll();
        for (Setmeal setmeal : list) {
            getSetmealDetailHtml(setmeal.getId());
        }
        return list;
    }

    /**
     * 移动端
     * 查询套餐检查组和检查项
     *
     * @param id
     * @return
     */
    @Override
    public Setmeal findDetailById(Integer id) {
        return setmealDao.findDetailById(id);
    }

    /**
     * 删除套餐组
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        //查找是否有setmeal对象
        Setmeal setmeal = setmealDao.findById(id);
        //判断对象是否为空
        if (setmeal != null) {
            //删除套餐和检查组中间表
            setmealDao.deleteAssocication(setmeal.getId());
        }
        setmealDao.delete(id);
    }

    /**
     * 创建根据模板页面,生成静态页面
     *
     * @param setmealId
     */
    public void getSetmealDetailHtml(Integer setmealId) {
        try {
            //获取配置对象&模板对象
            Template template = freeMarkerConfigurer.getConfiguration().getTemplate("mobile_setmeal_detail.ftl");
            //查找单个套餐对象
            Setmeal setmeal = findDetailById(setmealId);
            //创建数据模型
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("setmeal", setmeal);
            //创建字符输出流 ,输出
            template.process(map, new FileWriter("E:\\IdeaProjects\\health_parent\\health_mobile\\src\\main\\webapp\\pages\\setmeal_detail_" + setmealId + ".html"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
